﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using PagedList;

namespace LKVMLMADMIN.Controllers.Html
{
    public class ProvideHelpController : Controller
    {
        public LKVService.Transaction trans = new LKVService.Transaction();
        public ActionResult ListPH(int page = 1, string status = "", string Fromdate = "", string Todate = "", string Username = "", string message = "")
        {
            if (Session["User"] == null)
                return RedirectToAction("Login", "Account");
            ViewBag.Msg = message;
            DateTime fromdate;
            DateTime todate;
            int Stt = -1;
            if (Fromdate == "")
                fromdate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            else
            {
                fromdate = Convert.ToDateTime(Fromdate);
                ViewBag.DateFrom = Fromdate;
            }
            if (Todate == "")
                todate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);
            else
            {
                todate = Convert.ToDateTime(Todate);
                ViewBag.Todate = Todate;
            }
            if (status != "")
                Stt = Convert.ToInt32(status);
            var ls = trans.getListPHForAdmin(Username, Stt, fromdate, todate, (page - 1) * 25, 25);
            int totalItems = ls.Item1;
            var lsPH = ls.Item2.ToList();
            ViewBag.PHs = lsPH;
            int[] myArray = Enumerable.Repeat(-1, ls.Item1).ToArray();
            return View(myArray.ToPagedList(page, 25));
        }
        public ActionResult ListBadPH(int page = 1, string Fromdate = "", string Todate = "", string Username = "", string message = "")
        {
            if (Session["User"] == null)
                return RedirectToAction("Login", "Account");
            ViewBag.Msg = message;
            DateTime fromdate;
            DateTime todate;
            if (Fromdate == "")
                fromdate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            else
            {
                fromdate = Convert.ToDateTime(Fromdate);
                ViewBag.DateFrom = Fromdate;
            }
            if (Todate == "")
                todate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);
            else
            {
                todate = Convert.ToDateTime(Todate);
                ViewBag.Todate = Todate;
            }
            var ls = trans.getListPHForAdminIsBad(Username,fromdate, todate, (page - 1) * 25, 25);
            int totalItems = ls.Item1;
            var lsPH = ls.Item2.ToList();
            ViewBag.PHs = lsPH;
            int[] myArray = Enumerable.Repeat(-1, ls.Item1).ToArray();
            return View(myArray.ToPagedList(page, 25));
        }
        public ActionResult showModalPhInfo(int userId = 0)
        {
            var model = trans.findModalInfo(userId);
            return PartialView("_ShowInfoPH", model);
        }
        public ActionResult DeletePH(int phId = 0, int userId = 0)
        {
            if (Session["User"] == null)
                return RedirectToAction("Login", "Account");
            var kq = trans.deletePHOfUser(phId, userId);
            if(kq.Equals("***"))
                return RedirectToAction("ListPH", "ProvideHelp", new { message = "Success !!!" });
            else
                return RedirectToAction("ListPH", "ProvideHelp", new { message = kq });
        }
        public ActionResult LockUserPH(int transId = 0)
        {
            //tìm transfer order
            var ts = trans.findTransferOrderById(transId);
            if(ts == null)
                return RedirectToAction("ListBadPH", "ProvideHelp", new { message = "Cannot find Transfer Order" });
            var rs = trans.lockUserAndProccessParent(ts.Ph_Id.Value, ts.Id);
            if(rs.Equals("***"))
            {
                return View();            }
            else
              return RedirectToAction("ListBadPH", "ProvideHelp", new { message = "Faild ! Please try again." });
        }
    }
}
